﻿'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class RemoveContactTransaction
    Inherits BaseSSITransaction

    Private ReadOnly _Contact As IcqContact

    Public Sub New(ByVal owner As IcqStorageService, ByVal contact As IcqContact)
        MyBase.New(owner)
        _Contact = contact
    End Sub

    Public ReadOnly Property Contact() As IcqContact
        Get
            Return _Contact
        End Get
    End Property
    
    Public Overrides Function CreateSnac() As DataTypes.Snac
        Dim data As DataTypes.Snac130A
        Dim item As DataTypes.SSIBuddyRecord

        item = New DataTypes.SSIBuddyRecord

        item.ItemId = Contact.ItemId
        item.ItemName = Contact.Identifier
        item.GroupId = DirectCast(Contact.Group, IcqGroup).GroupId

        ' Create delete buddy snac
        data = New DataTypes.Snac130A
        data.BuddyRecords.Add(item)

        Return data
    End Function

    Public Overrides Sub OnComplete(ByVal action As DataTypes.SSIActionResultCode)
        Select Case action
            Case DataTypes.SSIActionResultCode.Success
                Service.InnerRemoveContactFromStorage(Contact)
            Case Else
                Throw New InvalidOperationException(String.Format("Cannot remove contact '{0}' {1}: {2}", Contact.Name, Contact.Identifier, action))
        End Select
    End Sub

    Public Overrides Sub Validate()
        If Contact.ItemId = 0 Then Throw New InvalidOperationException("Cannot remove a contact which is not a member of the contact list.")
    End Sub
End Class